// pages/user/find/releaseSun/releaseSun.js
const app = getApp();
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // currPickerItem: '请选择问题类型', //当前选中
    // currPickerIndex: '', //选中id
    index: '', //
    pickerItemArr: [],
    addPicList: [], //上传图片集
    imgarr: [],//图片集id
    searchFlag: true,
    isDel: false,
    chooseViewShow: true,
  },
  //点击输入标题
  toSearchKeyWord: function () {
    //点击输入框
    this.setData({
      searchFlag: false
    })
  },
  /** 选择上传图片 */
  uploadPic: function () {
    var that = this;
    let {
      isDel,
      addPicList,
      imgarr,
    } = that.data
    if (addPicList.length >= 0) {
      that.setData({
        isDel: true
      })
    }
    that.chooseViewShow()
    if (that.data.addPicList.length < 3) {
      wx.chooseImage({
        count: 5, //最多选择几张图片
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
          wx.showLoading({
            title: '正在上传中',
            mask: true,
          })
          let webSiteUrl = app.globalData.webSiteUrl
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          console.log(tempFilePaths[0])
          wx.uploadFile({
            url: webSiteUrl + '/app/Upload/upload', //接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {},
            success(res) {
              wx.hideLoading();
              var data = res.data
              data=JSON.parse(data);
              console.log(data)
                that.setData({
                  addPicList: addPicList.concat(data.data),
                  imgarr: imgarr.concat(data.data.id)
                })
            }
          })
        }
      })
      that.chooseViewShow()
    } else {
      wx.showToast({
        title: '限制选择3个文件',
        icon: 'loading',
        duration: 1000
      })
    }
  },
  /** 删除图片 */
  deleteImvAddPicList: function (e) {
    var addPicList = this.data.addPicList;
    var imgarr = this.data.imgarr;
    var itemIndex = e.currentTarget.dataset.index;
    addPicList.splice(itemIndex, 1);
    console.log(itemIndex);
    this.setData({
      addPicList: addPicList,
      imgarr:imgarr,
    })
    //判断是否隐藏选择图片
    this.chooseViewShow();
  },
  /** 是否隐藏图片选择*/
  chooseViewShow: function () {
    let {
      addPicList
    } = this.data
    if (addPicList.length >= 3) {
      this.setData({
        chooseViewShow: false
      })
    } else {
      this.setData({
        chooseViewShow: true
      })
    }
  },
  
  //请求提交反馈意见接口
  formSubmit: function (e) {
    // console.log('ss', e, this.data.typeId)
    var that = this;
    if (e.detail.value.title.length == 0) {
      wx.showModal({
        title: '提示',
        content: "发布标题不能为空",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    } else if (e.detail.value.suggest.length == 0) {
      wx.showModal({
        title: '提示',
        content: "发布内容不能为空",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    } else if(that.data.addPicList.length==0){
      wx.showModal({
        title: "提示",
        content: "图片为空",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    }else {
      that.getFeedbacksb(e);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //请求提交发布接口
  getFeedbacksb:function(e){
    var that = this;
    let uid=wx.getStorageSync('userid')
    var imgarr= that.data.imgarr;
    var param = {
      uid: uid.id,
      title: e.detail.value.title.toString(),
      content: e.detail.value.suggest.toString(),
      imgarr: imgarr.toString(),
    }
    console.log(param)
    Api.ajax("/app/Zarticle/addArticle", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '发布提交成功', //这里成功
          icon: 'success',
          duration: 1000
        });
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          });
        },1000)
      }else{
        wx.showToast({
          title: res.msg, //这里失败
          icon: 'none',
          duration: 1000
        })
      }
    })
  } ,
  
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})